package xiangmu;

import java.util.Scanner;


public class DingCan {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = 4;
        String[] names = new String[num];
        String[] dishMegs = new String[num];
        int[] times = new int[num];
        String[] addresses = new String[num];
        int[] states = new int[num];
        double[] sumprices = new double[num];

        String[] dishnames = {"红烧带鱼", "鱼香肉丝", "时令鲜蔬"};
        double[] prices = {38.0, 20.0, 10.0};
        int[] praiseNums = new int[3];

        names[0] = "张晴";
        dishMegs[0] = "红烧带鱼 2份";
        times[0] = 12;
        addresses[0] = "天成路207号";
        states[0] = 1;
        sumprices[0] = 76.0;

        names[1] = "张晴";
        dishMegs[1] = "鱼香肉丝 2份";
        times[1] = 18;
        addresses[1] = "天成路207号";
        states[1] = 0;
        sumprices[1] = 45.0;

        while (true) {
            //这一步为后面判断输出的数组下标最大值，省略了很多的重复步骤，非常好的思路
            int index = names.length;   //假设数组满了，即没有空位置
            for (int i = 0; i < names.length; i++) {
                if (names[i] == null) {
                    index = i;   //先确定了数组中的空位置，后续对于不同的操作不同的case,for循环不需要再判断name[i]是否为空的情况，直接i<index 即可
                    break;         //这样通过确定空位置的下标来控制后续输出时的下标上限，比前面我写的两个版本要更好
                }
            }

            System.out.println("欢迎使用吃货联盟订餐系统");
            System.out.println("---------------------------");
            System.out.println("1.我要订餐");
            System.out.println("2.查看餐袋");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("---------------------------");

            System.out.println("请选择:");
            int choose = input.nextInt();
            switch (choose) {
                case 1:
                    if (index == names.length)
                        System.out.println("餐袋已满，请及时处理");
                    else {
                        System.out.println("--新增订单--");
                        System.out.println("请输入订餐人姓名:");
                        names[index] = input.next();

                        System.out.println("序号\t名称\t\t单价\t点赞数");
                        for (int i = 0; i < dishnames.length; i++)
                            System.out.println((i + 1) + "\t" + dishnames[i] + "\t\t" + prices[i] + "\t" + praiseNums[i]);

                        System.out.println("选择菜名的序号:");
                        int caiNum = 0;
                        while ((caiNum = input.nextInt()) < 1 || caiNum > dishnames.length)
                            System.out.println("输入错误，请重新输入");
                        System.out.println("选择购买的份数:");
                        int fenNum = 0;
                        while ((fenNum = input.nextInt()) <= 0)
                            System.out.println("订餐份数必须大于0，请请重新输入:");
                        dishMegs[index] = dishnames[caiNum - 1] + " " + fenNum + "份";

                        System.out.println("请输入送达时间:");
                        int time = 0;
                        while ((time = input.nextInt()) < 8 || time > 20)
                            System.out.println("送餐时间必须在8-20之间,请重新输入:");
                        times[index] = time;

                        System.out.println("请输入送达地址:");
                        addresses[index] = input.next();

                        states[index] = 0;

                        sumprices[index] = prices[caiNum - 1] * fenNum;
                        sumprices[index] += sumprices[index] >= 50 ? 0 : 5;
                        System.out.println("订餐成功");
                    }

                    break;
                case 2:
                    System.out.println("--查看餐袋--");
                    System.out.println("序号\t订餐人\t\t餐品信息\t\t送达时间\t\t送达地址\t\t订单状态\t总金额");
                    for (int i = 0; i < index; i++) {   //不需要再判断数组哪一位为空，index是数组第一个为空的位置下标，这样判断更好更省步骤
                        String time = times[i] + "点";
                        String sumprice = sumprices[i] + "元";
                        String state;
                        if (states[i] == 0)
                            state = "已预订";
                        else
                            state = "已完成";
                        System.out.println((i + 1) + "\t" + names[i] + "\t\t" + dishMegs[i] + "\t\t" + time + "\t\t" + addresses[i] + "\t\t" + state + "\t" + sumprice);
                    }

                    break;
                case 3:
                    System.out.println("--签收订单--");
                    System.out.println("请输入你要签收的订单:");
                    int orderNum = 0;
                    while ((orderNum = input.nextInt()) < 1 || orderNum > index)
                        //orderNum可以等于index，index位置为数组第一个为空的位置下标。如果orderNum==index,(orderNum-1)位不为空.即:orderNum<=index
                        System.out.println("订单序号输入错误，请重新输入:");
                    if (states[orderNum - 1] == 0) {
                        states[orderNum - 1] = 1;
                        System.out.println("签收成功");
                    } else
                        System.out.println("该订单你已签收过了，不能重复签收");

                    break;
                case 4:
                    System.out.println("--删除订单--");
                    System.out.println("请选择你要删除的订单号:");
                    int delNum = 0;
                    while ((delNum = input.nextInt()) < 1 || delNum > index)
                        //orderNum可以等于index，index位置为数组第一个为空的位置下标。如果orderNum==index,(orderNum-1)位不为空.即:orderNum<=index
                        System.out.println("订单序号输入错误，请重新输入:");
                    if (states[delNum - 1] == 0)
                        System.out.println("该订单还在预定中，不能删除");
                    else {
                        for (int i = delNum - 1; i < index - 1; i++) {
                            names[i] = names[i + 1];
                            dishMegs[i] = dishMegs[i + 1];
                            times[i] = times[i + 1];
                            addresses[i] = addresses[i + 1];
                            states[i] = states[i + 1];
                            sumprices[i] = sumprices[i + 1];
                        }
                        //while开头初始条件是index==names.length。switch里面的每个case都是相互独立的，且只会执行一个case。
                        //考虑如果数组满了的情况下，如果删除数组中的一个元素，采取上面的覆盖删除法，要对最后一个数组元素置空
                        //如果数组满了，index-1==length-1
                        names[index - 1] = null;
                        dishMegs[index - 1] = null;
                        times[index - 1] = 0;
                        addresses[index - 1] = null;
                        states[index - 1] = 0;
                        sumprices[index - 1] = 0;

                        System.out.println("删除完成");
                        index--;    //while语句循环多次，index 动态变化，index表示数组第一个为空的下标，删除一个元素，index要减1
                    }

                    break;
                case 5:
                    System.out.println("--我要点赞--");
                    System.out.println("序号\t菜名\t\t单价\t点赞数");
                    for (int i = 0; i < dishnames.length; i++)
                        System.out.println((i + 1) + "\t" + dishnames[i] + "\t\t" + prices[i] + "\t" + praiseNums[i]);
                    System.out.println("请输入要点赞的菜名序号:");
                    int praiseNum = 0;
                    while ((praiseNum = input.nextInt()) < 1 || praiseNum > dishnames.length)
                        System.out.println("点赞的菜名序号错误，请重新输入:");
                    praiseNums[praiseNum - 1]++;
                    System.out.println("点赞后的菜品展示:");
                    System.out.println("序号\t菜名\t\t单价\t点赞数");
                    for (int i = 0; i < dishnames.length; i++)
                        System.out.println((i + 1) + "\t" + dishnames[i] + "\t\t" + prices[i] + "\t" + praiseNums[i]);

                    break;
                case 6:
                    System.out.println("--退出系统--");
                    System.out.println("谢谢使用，欢迎下次光临");
                    return; //用return彻底退出循环，但也会导致循环外的语句也不会再执行。return语句不是跳出所有循环，而是从return语句始，就彻底停止执行后面所有语句。
                default:
                    System.out.println("选择序号错误");
                    break;
            }
            System.out.println("输入0返回:");
            int number = input.nextInt();
            if (number == 0)
                continue;
            else
                break;
        }
        System.out.println("谢谢使用，欢迎下次光临");
    }

}
